package com.lagou.web.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class LoginFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        // 类型向下转型
        HttpServletRequest request=  (HttpServletRequest) servletRequest;
        HttpServletResponse response =  (HttpServletResponse) servletResponse;
        // 1.通过rquest对象，获取session对象
        HttpSession session = request.getSession();
        // 2.操作session的API，获取数据
        String username = (String) session.getAttribute("username");
        String password = (String) session.getAttribute("password");
        Object pass =  session.getAttribute("pass");
        System.out.println("测试不存在cookie返回值为："+pass);
        System.out.println("GetSession获取：" + username + "；" + password);


    }

    @Override
    public void destroy() {

    }
}
